package com.springboot.learning.project.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.springboot.learning.framework.web.page.PaginationList;
import com.springboot.learning.framework.web.page.PaginationSupport;
import com.springboot.learning.project.entity.po.User;
import com.springboot.learning.project.mapper.UserMapper;
import com.springboot.learning.project.service.UserService;


/**
*
* User 表数据服务层接口实现类
*
*/
@Service
public class UserServiceImpl  extends ServiceImpl<UserMapper, User> implements UserService {

	 @Autowired
	 private SqlSessionFactory factory;
	 
	@Override
    public int setStatus(Integer userId, int status) {
        return this.baseMapper.setStatus(userId, status);
    }

    @Override
    public int changePwd(Integer userId, String pwd) {
        return this.baseMapper.changePwd(userId, pwd);
    }

    @Override
    public List<Map<String, Object>> selectUsers(String name, String beginTime, String endTime, Integer deptid) {
        return this.baseMapper.selectUsers(name, beginTime, endTime, deptid);
    }

    @Override
    public int setRoles(Integer userId, String roleIds) {
        return this.baseMapper.setRoles(userId, roleIds);
    }

    /**
     * mybatis的一级进行测试，只查询了一次
     * @param id
     * @return
     */
    @Override
    public User selectUserById(Integer userId) {
        // 自动提交事务
        SqlSession sqlSession = factory.openSession(true);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Map<String,Object> paramsMap = new HashMap<>(16);
        paramsMap.put("id",userId);
        User rmsUser = userMapper.selectOneUser(paramsMap);
        userMapper.selectOneUser(paramsMap);
        userMapper.selectOneUser(paramsMap);
        userMapper.selectOneUser(paramsMap);
        return rmsUser;
//        return cacheService.selectUserById(id);
    }
    
    @Override
    public User selectUserByAccout(String account) {
    	 Map<String,Object> paramsMap = new HashMap<>(5);
         paramsMap.put("account",account);
         User user = baseMapper.selectOneUser(paramsMap);
         return user;
    }

	@Override
	public void addOrUpdateUser(User user) {
		 // 添加
        if (user!=null && user.getId()!=null && user.getId()!=0) {
        	 Map<String,Object> paramsMap = new HashMap<>(5);
             paramsMap.put("account",user.getAccount());
             User theUser = baseMapper.selectOneUser(paramsMap);
            if (theUser!=null) {
            	//插入
            }else{
            	insert(user);
            }
            	
        } else { 
        	//修改
        	updateById(user);
        }
	}

	@Override
	public List<User> selectByCondition(User user) {
		return baseMapper.selectList(createConditions(user));
	}

	@Override
	public PaginationList selectByPageCondition(PaginationSupport ps, User user) {

		int currentPage = ps.getCurrentPage();
		int pageSize = ps.getPageSize();

		PaginationList pl = new PaginationList();
		pl.setCurrentPage(currentPage);
		pl.setPageSize(pageSize);
		Wrapper<User> conditions = createConditions(user);
		int totalSize = 0;
		if (ps.isRequiredTotalSize()) {
			totalSize = baseMapper.selectCount(conditions);
			pl.setTotalSize(totalSize);
		}
		if (totalSize > 0 || !ps.isRequiredTotalSize()) {
			Page<User> page = new Page<>(currentPage, pageSize);
			List<User> list = this.baseMapper.selectPage(page, conditions);
			pl.setCollection(list);
		} else {
			pl.setCollection(new ArrayList<>());
		}
		pl.setCurrentPage(currentPage);
		return pl;
	}

	private Wrapper<User> createConditions(User user){
		EntityWrapper<User> conditions=new EntityWrapper<User>();
 		if(user!=null){
			if(StringUtils.isNotBlank(user.getName())){
				conditions.like("name",user.getName(),SqlLike.RIGHT);
			}
		}
		return conditions;
	}
}

